<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 消消乐游戏（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>游戏规则&#xff1a;输入一个只包含英文字母的字符串&#xff0c;字符串中的两个字母如果相邻且相同&#xff0c;就可以消除。</p> 
<p>在字符串上反复执行消除的动作&#xff0c;直到无法继续消除为止&#xff0c;此时游戏结束。</p> 
<p>输出最终得到的字符串长度。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入原始字符串 str &#xff0c;只能包含大小写英文字母&#xff0c;字母的大小写敏感&#xff0c; str 长度不超过100。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出游戏结束后&#xff0c;最终得到的字符串长度。</p> 
<p></p> 
<h4>备注</h4> 
<p>输入中包含 非大小写英文字母 时&#xff0c;均为异常输入&#xff0c;直接返回 0。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:47px;">输入</td><td style="width:451px;">gg</td></tr><tr><td style="width:47px;">输出</td><td style="width:451px;">0</td></tr><tr><td style="width:47px;">说明</td><td style="width:451px;">gg 可以直接消除&#xff0c;得到空串&#xff0c;长度为0。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td>mMbccbc</td></tr><tr><td>输出</td><td>3</td></tr><tr><td>说明</td><td> <p>在 mMbccbc 中&#xff0c;可以先消除 cc &#xff1b;</p> <p>此时字符串变成 mMbbc &#xff0c;可以再消除 bb &#xff1b;</p> <p>此时字符串变成 mMc &#xff0c;此时没有相邻且相同的字符&#xff0c;无法继续消除。</p> <p>最终得到的字符串为 mMc &#xff0c;长度为3。</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>这题比较容易地解法是利用栈结构。</p> 
<p>即先创建一个栈&#xff0c;然后遍历输入的字符串&#xff0c;将每一个字符尝试压入栈&#xff0c;但是压入前&#xff0c;需要判断&#xff0c;栈顶元素是否和将要压入的字符相同&#xff0c;若不同&#xff0c;则压入&#xff0c;若相同&#xff0c;则字符不压入&#xff0c;且栈顶元素弹出。</p> 
<blockquote> 
 <p>由于这题只要遇到两两相邻相同的字符就会消除&#xff0c;比如acccb&#xff0c;消除后就变为了acb&#xff0c;因此遇到&gt;2的奇数个相同相邻字符&#xff0c;是无法消除完的&#xff0c;总是会遗留一个。</p> 
</blockquote> 
<p></p> 
<p>注意&#xff0c;遍历的字符如果是非字母&#xff0c;则直接返回0。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLine()));
  }

  public static int getResult(String s) {
    LinkedList&lt;Character&gt; stack &#61; new LinkedList&lt;&gt;();

    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      char c &#61; s.charAt(i);

      if (c &lt; &#39;A&#39; || c &gt; &#39;z&#39; || (c &gt; &#39;Z&#39; &amp;&amp; c &lt; &#39;a&#39;)) return 0;

      if (stack.size() &gt; 0 &amp;&amp; c &#61;&#61; stack.getLast()) stack.removeLast();
      else stack.addLast(c);
    }

    return stack.size();
  }
}
</code></pre> 
<p> </p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  console.log(getResult(line));
});

function getResult(s) {
  const stack &#61; [];

  for (let c of s) {
    if (c &lt; &#34;A&#34; || c &gt; &#34;z&#34; || (c &gt; &#34;Z&#34; &amp;&amp; c &lt; &#34;a&#34;)) return 0;

    if (stack.length &gt; 0 &amp;&amp; stack.at(-1) &#61;&#61; c) stack.pop();
    else stack.push(c);
  }

  return stack.length;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()


# 算法入口
def getResult():
    stack &#61; []

    for c in s:
        if c &lt; &#39;A&#39; or c &gt; &#39;z&#39; or &#39;Z&#39; &lt; c &lt; &#39;a&#39;:
            return 0

        if len(stack) &gt; 0 and stack[-1] &#61;&#61; c:
            stack.pop()
        else:
            stack.append(c)

    return len(stack)


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>